package bill.study.designPattern.iterator.filecontent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class FileAsIterableString implements Iterable<String>{
	private List<String> lines = new ArrayList<String>() ;
	public FileAsIterableString(final File file){
		try {
			BufferedReader reader = new BufferedReader (new FileReader(file));
			String line = reader.readLine();
	
			while(line != null){
				lines.add(line);
				line = reader.readLine();
			}
		}
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public Iterator<String> iterator() {
		// TODO Auto-generated method stub
		return lines.iterator();
	}

	
	public static void main(String [] args){
		File file = new File("C:\\Documents and Settings\\yangliu.APPLICATIONS\\accountedInvoice.sql");
		FileAsIterableString i = new FileAsIterableString(file);

		/*for(Iterator<String> it =i.iterator(); it.hasNext();){
			System.out.println(it.next());
		}*/
		
		for(String str: new FileAsIterableString(file)){
			System.out.println(str);
		}
		
//		
//		String [] sqlFiles =file.list(new FilenameFilter(){
//
//			@Override
//			public boolean accept(File dir, String name) {
//				if(name.endsWith("sql"))
//					return true;
//				return false;
//			}
//			
//		});
//		
//		for(int i = 0; i < sqlFiles.length; i ++){
//			System.out.println(sqlFiles[0]);
//			
//		}
		
	}
}
